//
//  BaseViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/15.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = ColorFrom16(0xeef4f7);
    self.navigationController.navigationBar.backgroundColor = ColorWhite;
    [UINavigationBar appearance].barTintColor=ColorWhite;
    self.edgesForExtendedLayout = UIRectEdgeNone;//////作用：使导当前的view的高度从导航条下开始（0，0）开始
    self.view.backgroundColor = ColorWhite;
    [[UINavigationBar appearance] setTranslucent:NO];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                 style:UIBarButtonItemStyleDone
                                                                target:nil
                                                                action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];
    
    // 返回手势、handleNavigationTransition: 系统方法
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString([NSString stringWithFormat:@"hand%@vigat%@rans%@n:",@"leNa",@"ionT",@"itio"])];
    edgePanGestureRecognizer.delegate = self;
    edgePanGestureRecognizer.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePanGestureRecognizer];
    
    // 禁止系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    // 当当前控制器是根控制器时，不可以侧滑返回，所以不能使其触发手势
    if(self.navigationController.childViewControllers.count == 1)
    {
        return NO;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    return YES;
}

#pragma mark - 添加返回按钮
- (void)addBackItem {
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(backItemAction)];
}

- (void)backItemAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加导航条左按钮（如：Cancel）
- (void)addLeftBarButtonWithTitle:(NSString *)title andSel:(SEL)sel{
    
    UIButton *btn = [self createButtonWithTitle:title andSel:sel];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

#pragma mark - 添加导航条右按钮（如：Edit）
- (void)addRightBarButtonWithTitle:(NSString *)title andSel:(SEL)sel{
    
    UIButton *btn = [self createButtonWithTitle:title andSel:sel];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (UIButton *)createButtonWithTitle:(NSString *)title andSel:(SEL)sel {
    
    float btnWidth = [GW_Tool sizeOfLabelWithString:title font:PFRFONT(15) width:MAXFLOAT].width;
    if (btnWidth < 40) {
        btnWidth = 40;
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(0, 0, btnWidth, 44);
    btn.exclusiveTouch = YES;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = PFRFONT(15);
//    [btn setTitleColor:COLOR_FOR_NAVI_TINT forState:UIControlStateNormal];
//    [btn setTitleColor:ColorFrom16(0x36405b) forState:UIControlStateDisabled];
    [btn setTitleColor:ColorFrom16(0x4a4a4a) forState:0];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

- (void)configNavigation:(NSString *)title 
{
    self.navigationItem.hidesBackButton = YES;
    UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 64)];
    navigationView.backgroundColor = ColorWhite;
    [self.view addSubview:navigationView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, GWIDTH, 44)];
    label.text = title;
    label.textColor = ColorFrom16(0x202020);
    label.font = PFRFONT(18);
    label.textAlignment = 1;
    [navigationView addSubview:label];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:IMAGE(@"nav_back@2x") forState:0];
    backBtn.frame = CGRectMake(10, 30, 20, 20) ;
    [backBtn addTarget:self action:@selector(handleBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:backBtn];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, GWIDTH, 1)];
    view.backgroundColor = ColorFrom16(0xe2e2e2);
    [self.view addSubview:view];
}

- (void)handleBackBtn:(UIButton *)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 返回首页
- (void)backToHomeTab:(NSInteger)tabIndex {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homeChange" object:nil];

    if (self.navigationController.viewControllers.count > 0) {
        if ([self.navigationController.viewControllers[0] tabBarController].selectedIndex != tabIndex) {
            [[self.navigationController.viewControllers[0] tabBarController] setSelectedIndex:tabIndex];
        }
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)noNetwork
{
    LoadingView *load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 64)];
    [load setReminderLabelTitle:@"糟糕！断网了" andImage:@"noNetweok"];
    [load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:load];
}


- (void)viewWillAppear:(BOOL)animated
{
    [GW_Request sharedInstance];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    NSLog(@"销毁了!");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
